package com.cdm.forum.service.impl;

import com.cdm.forum.common.Result;
import com.cdm.forum.common.ResultCode;
import com.cdm.forum.dao.MessageMapper;
import com.cdm.forum.exception.ApplicationException;
import com.cdm.forum.model.Message;
import com.cdm.forum.model.User;
import com.cdm.forum.service.IMessageService;
import com.cdm.forum.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class MessageServiceImpl implements IMessageService {

    @Resource
    private MessageMapper messageMapper;

    @Resource
    private IUserService userService;

    @Override
    public void create(Message message) {
        //非空校验
        if(message == null || message.getPostUserId() == null || message.getReceiveUserId() == null
                || StringUtils.isEmpty(message.getContent())){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //校验接收者在不在
        User user = userService.selectById(message.getPostUserId());
        if(user == null){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //设置默认值
        message.setState((byte) 0);
        message.setDeleteState((byte) 0);

        //设置创建和更新时间
        Date now = new Date();
        message.setCreateTime(now);
        message.setUpdateTime(now);

        //调用mapper
        int row = messageMapper.insertSelective(message);
        if(row != 1){
            log.warn(ResultCode.FAILED_CREATE .toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_CREATE ));
        }

    }

    @Override
    public Integer selectUnreadCount(Long receiveUserId) {
        //非空校验
        if(receiveUserId == null || receiveUserId <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //调用mapper
        Integer count = messageMapper.selectUnreadCount(receiveUserId);
        return count;

    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
        //非空校验
        if(receiveUserId == null || receiveUserId <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //调用mapper
        List<Message> messages = messageMapper.selectByReceiveUserId(receiveUserId);
        return messages;
    }

    @Override
    public void updateStatus(Long id, Byte status) {
        //非空校验 status : 0未读 1已读 2 已回复
        if(id == null || id <= 0 || status == null|| status < 0 || status >= 3){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //构造更新对象
        Message uptatemessage = new Message();
        uptatemessage.setId(id);
        uptatemessage.setState(status);
        uptatemessage.setUpdateTime(new Date());

        //调用mapper
        int row = messageMapper.updateByPrimaryKeySelective(uptatemessage);
        if(row != 1){
            //打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString());
            //抛出异常
            throw new ApplicationException(Result.failed(ResultCode.ERROR_SERVICES ));
        }
    }

    @Override
    public Message selectById(Long id) {
        //非空校验
        if(id == null || id <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //调用mapper
        Message message = messageMapper.selectByPrimaryKey(id);
        return message;
    }

    @Override
    public void reply(Long repliedId, Message message) {
        if(repliedId == null || repliedId <= 0){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //校验replyUserId对应的站内信
        Message existsMessage1 = messageMapper.selectByPrimaryKey(repliedId);
        if(existsMessage1 == null || existsMessage1.getDeleteState() == 1){
            log.warn(ResultCode.FAILED_MESSAGE_NOT_EXISTS.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS));
        }

        //更新状态为已回复
        updateStatus(repliedId, (byte) 2);

        //回复的内容写入数据库
        create(message);
    }
}
